package www.cskaoyan.com.dao;

import www.cskaoyan.com.bean.ClassManager;
import www.cskaoyan.com.dao.JDBCUtils.ConnectionPool;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassManagerDao {

    Connection connection;


    public void create(String className, String beginDate) throws SQLException {
        connection = ConnectionPool.getConnection();

        String sql = "insert into `class` set  className = ? , beginDate = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, className);
        preparedStatement.setString(2, beginDate);
        preparedStatement.executeUpdate();

        return ;
    }

    // 获取班级列表
    public List<ClassManager> getClassList() throws SQLException {
        connection = ConnectionPool.getConnection();
        String sql = "select *  from `class` ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();


        List<ClassManager> list = new ArrayList<>();
        while (resultSet.next()){

            int id = resultSet.getInt(1);
            String className  = resultSet.getString(2);
            String beginDate = resultSet.getDate(3).toString();

            ClassManager classManager = new ClassManager(id, className, beginDate);
            list.add(classManager);
        }

        return list;
    }


    // 根据id删除班级信息
    public void deleteClass(int classId) throws SQLException {
        connection = ConnectionPool.getConnection();

        String sql = "delete from `class` where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, classId);
        preparedStatement.executeUpdate();

        return ;
    }
}
